package org.marsdonne.auth;

import org.marsdonne.archives.Client;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.security.oauth2.jwt.JwtDecoder;

import java.time.Instant;
import java.util.LinkedHashMap;
import java.util.Map;

import static org.springframework.core.Ordered.HIGHEST_PRECEDENCE;

@TestConfiguration
public class MockJwtConfigurer {
    static final String TOKEN = "token";
    static final String SUB = "sub";
    public static final String USER_USERNAME = "superman";
    public static final Long USER_ID = 1L;
    public static final Long USER_ORG_ID = 1L;
    public static final String USER_ORG_NAME = "DC";
    public static final String USER_ORG_LICENSE_CODE = "DC1234567890123456";
    public static final String USER_REAL_NAME = "Kal-EI";
    public static final Client USED_CLIENT = Client.C;

    @Bean
    @Order(HIGHEST_PRECEDENCE)
    public JwtDecoder jwtDecoder() {
        return token -> jwt();
    }

    public Jwt jwt() {
        Map<String, Object> claims = new LinkedHashMap() {{
            put(User.Constants.USER_ID, USER_ID);
            put(SUB, USER_USERNAME);
            put(User.Constants.USER_REAL_NAME, USER_REAL_NAME);
            put(User.Constants.USED_CLIENT, USED_CLIENT);
            put(User.Constants.USER_ORG_ID, USER_ORG_ID);
            put(User.Constants.USER_ORG_NAME, USER_ORG_NAME);
            put(User.Constants.USER_ORG_LICENSE_CODE, USER_ORG_LICENSE_CODE);
        }};

        return new Jwt(
                TOKEN,
                Instant.now(),
                Instant.now().plusSeconds(30),
                new LinkedHashMap() {{
                    put("alg", "none");
                }},
                claims
        );
    }
}
